#include <stdint.h>
#include <avr/interrupt.h>
#include "display.h"

extern volatile uint16_t gametime;
extern volatile uint16_t huntdelay;
extern volatile uint16_t huntblink;
extern uint16_t BallSpeed;  		
extern volatile uint16_t BallDelay;			

void LedCLS()
{
	/* "Clear Screen" */
	for (int8_t i = 0; i < SWIDTH; gLEDs[i++] = 0);
}

SIGNAL(SIG_OVERFLOW0)
{
	uint16_t ledval;
	uint8_t portdout;
	uint8_t portcout;

	static uint8_t col = 0;

	cli();							

	if (col == SWIDTH) col = 0;

	PORTB = 0;
	portdout = PORTD & 0b00001111;  // save lower first 4 bits and zero higher 4
	PORTD = portdout;
	portcout = PORTC & 0b11110000;  // save hiher first 4 bits and zero lower 4
	PORTC = portcout;

	if ( col == 0 )	PORTB &= ~(1 << 4); 
	else  			PORTB |= (1 << 4);

	PORTB |= (1 << 3);
	PORTB &= ~(1 << 3);

	PORTB |= (1 << 2);
	PORTB &= ~(1 << 2);

	ledval = gLEDs[col++];
	
	PORTC = (ledval & 0x000f) | portcout;
	PORTD = (ledval & 0x00f0) | portdout;
	PORTB = (ledval >> 8) & 0x0003;

	if (BallDelay) BallDelay--;	   	/* counter decrease as long as it's not 0 */
	if (huntdelay) huntdelay--;
	if (huntblink) huntblink--;
	if (gametime) gametime--;

	sei();
}
